<?php

namespace app\admin\command;

use app\common\library\User;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Exception;
use think\Log;
use think\cache\driver\Redis;

class UpgradeExpertTaskChecked extends Command
{

    protected $_sleep = 30;
    // 休眠间隔
    protected $_doing = false;
    // 是否有数据正在处理
    protected $_tasks = [];

    protected function configure()
    {
        $this->setName('UpgradeExpertTaskChecked')->setDescription('升级达人任务检测');
    }

    /**
     * 休眠
     *
     * @param string $second            
     */
    public function sleep($second = "")
    {
        $second = $second ? $second : $this->_sleep;
        sleep(sleep($second)); // TP5的命令行 sleep($second) 不生效
        echo "sleep {$second} seconds ok!time:" . date('Y-m-d H:i:s') . PHP_EOL;
    }


    protected function execute(Input $input, Output $output)
    {

        while (true && ! $this->_doing) {
            $this->_doing = true;
            $this->autoRun();

            $this->_doing = false;
        }
    }

    /**
     * 自动执行
     *
     * @return bool
     */
    protected function autoRun()
    {
        $this->getWorks();
        if ($this->_tasks) {
            foreach ($this->_tasks as $user) {
                echo "start run user:" . $user["mobile"] . PHP_EOL;
                $result = $this->runTask($user);
                if ($result) {
                    //更新已完成任务
                    db("user")->where("id", $user["id"])->update([
                        "is_upgraded_expert_level" => 1,
                        "upgradeing_last_check_time" => time(),
                    ]);
                } else {
                    //更新检测时间
                    db("user")->where("id", $user["id"])->update([
                        "upgradeing_last_check_time" => time(),
                    ]);
                }
                echo "end run user:" . $user["mobile"] . PHP_EOL;
            }
        } else {
            $this->sleep(3);
        }
    }


    /**
     * 执行单个任务
     */
    protected function runTask($user)
    {
        try {
            $count = db("expert_upgrade_order")->where("user_id",$user["id"])->whereIn("upgrade_type", [1, 2, 3])->whereIn("status", [0, 1, -1, -2])->count();
            // exit(db()->getLastSql());
            if ($count > 0) {
                return false;
            } else {
                return true;
            }
        } catch (Exception $e) {
            Log::write($e->getMessage());
        }
    }


    /**
     * 获取升级任务
     */
    protected function getWorks()
    {
        try {
            $left_span = time() - 60;
            $user_list = db("user")->where("upgradeing_expert_level", ">", 0)->where("is_upgraded_expert_level", 0)->whereRaw("upgradeing_last_check_time=0 or upgradeing_last_check_time<" . $left_span)->order("upgradeing_last_check_time", "asc")->limit(1)->select();
            // exit(print_r($user_list ));
            // exit(db()->getLastSql());
            return $this->_tasks = $user_list ? $user_list : [];
        } catch (Exception $e) {
            Log::write($e->getMessage());
        }
    }
}
